package com.java110.community.cmd.ownerRepair;

import com.alibaba.fastjson.JSONObject;
import com.java110.community.cmd.community.UpdateCommunityCmd;
import com.java110.community.dao.IRepairSettingServiceDao;
import com.java110.core.annotation.Java110Cmd;
import com.java110.core.context.ICmdDataFlowContext;
import com.java110.core.event.cmd.Cmd;
import com.java110.core.event.cmd.CmdEvent;
import com.java110.core.log.LoggerFactory;
import com.java110.dto.repair.RepairDto;
import com.java110.dto.user.UserDto;
import com.java110.intf.community.IRepairInnerServiceSMO;
import com.java110.intf.community.IRepairSettingInnerServiceSMO;
import com.java110.intf.user.IUserInnerServiceSMO;
import com.java110.utils.exception.CmdException;
import com.java110.vo.ResultVo;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import java.text.ParseException;
import java.util.List;

@Java110Cmd(serviceCode = "ownerRepair.listOwnerRepairs")
public class ListOwnerRepairsCmd extends Cmd {
    private static Logger logger = LoggerFactory.getLogger(UpdateCommunityCmd.class);
    @Autowired
    private IRepairInnerServiceSMO repairInnerService;
    @Autowired
    private IUserInnerServiceSMO userInnerService;

    @Override
    public void validate(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson) throws CmdException, ParseException {

    }

    @Override
    public void doCmd(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson) throws CmdException, ParseException {
        logger.info("保存保修配置请求入参：{}", reqJson.toJSONString());
        //保存保修配置
        String userId = reqJson.getString("userId");
        String[] ids = {userId};
        List<UserDto> userInfo = userInnerService.getUserInfo(ids);
        String levelCd = userInfo.get(0).getLevelCd();
        if (!("00".equals(levelCd))) {
            //员工
            reqJson.put("staffId", userId);
        }
        List<RepairDto> repairs = repairInnerService.listOwnerRepairs(reqJson);
        int count = repairs.size();
        ResultVo resultVo = new ResultVo((int) Math.ceil((double) count / (double) reqJson.getInteger("row")), count, repairs);
        ResponseEntity<String> responseEntity = new ResponseEntity<String>(resultVo.toString(), HttpStatus.OK);
        context.setResponseEntity(responseEntity);
    }
}
